package com.crm.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.mapper.BusinessMapper;
import com.crm.mapper.ClueMapper;
import com.crm.pojo.common.ErrorMessage;
import com.crm.pojo.common.PageResult;
import com.crm.pojo.common.UserContext;
import com.crm.pojo.domain.Business;
import com.crm.pojo.domain.Clue;
import com.crm.pojo.domain.ClueRecord;
import com.crm.pojo.domain.LoginUser;
import com.crm.pojo.dto.CluePoolDto;
import com.crm.pojo.dto.ClueDto;
import com.crm.pojo.dto.ClueFalseDto;
import com.crm.pojo.vo.ClueRecordVo;
import com.crm.pojo.vo.ClueVo;
import com.crm.service.BusinessService;
import com.crm.service.ClueService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ClueServiceImpl  extends ServiceImpl<ClueMapper, Clue> implements ClueService {
    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private BusinessService businessService;
    @Autowired
    private BusinessMapper businessMapper;

    @Override
    public PageResult list(ClueDto clueDto) {
        //用pagehelper分页
        //1.设置分页参数
        PageHelper.startPage(clueDto.getPageNum(),clueDto.getPageSize());
        //2.查询数据
        List<ClueVo> list = clueMapper.list(clueDto);
        //3.封装数据
        Page page = (Page) list;
        PageResult<ClueVo,Object> pageResult = new PageResult<>();
        pageResult.setTotal(page.getTotal());
        pageResult.setRows(list);
        pageResult.setCode(200);
        pageResult.setMsg("查询成功");
        pageResult.setParams(null);
        return  pageResult;
    }
    //线索查找（id）
    @Override
    public ClueVo getById(Long id) {
        return clueMapper.getById(id);
    }

    @Override
    public void add(ClueDto clueDto) {
        Clue clue = new Clue();
        BeanUtils.copyProperties(clueDto,clue);
        clue.setOwner(UserContext.getCurrentUser().getUserName());
        clue.setOwnerTime(LocalDateTime.now().toString());
        clue.setCreateTime(LocalDateTime.now());
        clue.setUpdateTime(LocalDateTime.now());
        clue.setCreateBy(UserContext.getCurrentUser().getUserName());
        clue.setUpdateBy(UserContext.getCurrentUser().getUserName());
        try {
            clueMapper.add(clue);
        } catch (Exception e) {
            log.info(e.getMessage());
            throw new RuntimeException(ErrorMessage.CLUE_ERROR_MESSAGE_ADD_001);
        }
    }
    //线索跟进
    @Override
    public void record(ClueDto clueDto) {
        //补全信息
        Clue clue = new Clue();
        BeanUtils.copyProperties(clueDto,clue);
        clue.setId(clueDto.getClueId());
        clue.setNextTime(clueDto.getNextTime());
        clue.setUpdateTime(LocalDateTime.now());
        clue.setStatus("2");

        clue.setUpdateBy(UserContext.getCurrentUser().getUserName());


        ClueRecord clueRecord = new ClueRecord();
        clueRecord.setClueId(clueDto.getClueId());
        clueRecord.setSubject(clueDto.getSubject());
        clueRecord.setRecord(clueDto.getRecord());
        clueRecord.setLevel(clueDto.getLevel().toString());
        clueRecord.setNextTime(clueDto.getNextTime());
        clueRecord.setCreateBy(UserContext.getCurrentUser().getUserName());
        clueRecord.setCreateTime(LocalDateTime.now());
        try {
            clueMapper.record(clue);
            clueMapper.addRecord(clueRecord);
        } catch (Exception e) {
            log.info(e.getMessage());
            throw new RuntimeException(ErrorMessage.CLUE_ERROR_MESSAGE_UPDATE_001);
        }

    }
    //线索跟进记录
    @Override
    public PageResult clueRecord(Long clueId) {
        PageResult pageResult = new PageResult<>();
        List<ClueRecordVo> list = clueMapper.clueRecord(clueId);
        pageResult.setRows(list);
        pageResult.setTotal((long)list.size());
        pageResult.setCode(200);
        pageResult.setMsg("查询成功");
        return pageResult;
    }
    //伪线索
    @Override
    @Transactional
    public String clueFalse(ClueFalseDto clueFalseDto) {
        Long clueId = clueFalseDto.getClueId();
        ClueVo clueVo = clueMapper.getById(clueId);
        if (clueVo.getFalseCount()+1 == 3){
            clueMapper.myDeleteById(clueId);
            return "已经成为伪线索3次，将会删除该线索";
        }
        clueMapper.falseCount(clueId);
        clueMapper.falseReson(clueFalseDto);
        return "已成功转伪线索";
    }
    //线索批量分配
    @Override
    public String assignment(CluePoolDto cluePoolDto) {
        clueMapper.assignment(cluePoolDto);
        return "已全部分配";
    }
    //线索池批量捞取
    @Override
    public String gain(CluePoolDto cluePoolDto) {
        clueMapper.gain(cluePoolDto);
        return "捞取成功";
    }
    @Transactional
    @Override
    public String changeBusiness(Long id) {
        //现获取对应的线索
        ClueVo clueVo = clueMapper.getById(id);
        if (clueVo.getStatus().equals("1")){
            throw new RuntimeException(ErrorMessage.CLUE_ERROR_MESSAGE_TOBUSINESS_001);
        }
        //获取当前的用户
        LoginUser loginUser = UserContext.getCurrentUser();
        //组装商机对象
        Business business = new Business();
        BeanUtils.copyProperties(clueVo,business);
        business.setId(null);
        business.setOwner(loginUser.getUserName());
        business.setCreateBy(loginUser.getUserName());
        business.setUpdateBy(loginUser.getUserName());
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());
        business.setIsPool(0);
        business.setStatus(1);
        businessService.save(business);
        clueMapper.endById(id);
        return "转商机成功";
    }
    //线索转派
    @Override
    public void transfer(Long clueOrBusiness,Long oldId, Long newId) {
        if (clueOrBusiness == 0){
            clueMapper.transfer(oldId,newId);
            return;
        }
        if (clueOrBusiness == 1){
            businessMapper.transfer(oldId,newId);
            return;
        }
    }
}
